package com.rv.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rv.admin.domain.User;
import com.rv.admin.domain.bo.LoginBo;
import com.rv.admin.helper.LoginHelper;
import com.rv.admin.mapper.UserMapper;
import com.rv.admin.service.IUserService;
import com.rv.common.core.util.RsaUtil;
import com.rv.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;


@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Override
    public R<Page<User>> pageList(Page<User> page, User user) {
        Long id = LoginHelper.getLoginUser().getId();
        if (user == null) {
            user = new User();
        }
        user.setId(id);
        return R.ok(baseMapper.queryUserList(page, user));
    }

    @Override
    public R<User> register(User user) {
        if (user == null) {
            return R.error("用户信息为空");
        }
        if (user.getUsername() == null || user.getUsername().isEmpty()) {
            return R.error("用户名不能为空");
        }
        if (user.getPassword() == null || user.getPassword().isEmpty()) {
            return R.error("密码不能为空");
        }
        if (checkDuplicateUsername(user)) {
            return R.error("用户名已存在");
        }
        if (checkDuplicateNickname(user)) {
            return R.error("昵称已存在");
        }
        user.setState(0);
        user.setCreateTime(DateUtil.formatDateTime(new Date()));
        user.setPassword(user.getPassword());
        save(user);
        return R.ok(user);
    }

    @Override
    public R<User> login(LoginBo loginBo) {
        // 解密
        try {
            LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.eq(User::getUsername, loginBo.getUsername());
            User user = getOne(lambdaQuery);
            if (user == null) {
                return R.error("用户不存在");
            }
            if (!user.getPassword().equals(loginBo.getPassword())) {
                return R.error("密码错误");
            }
            // 状态在线
            user.setState(1);
            // 更新
            updateById(user);
            user.setPassword(null);
            // 登录成功
            LoginHelper.login(user);
            return R.ok(user);
        } catch (Exception e) {
            return R.error("用户名或密码错误");
        }
    }


    /**
     * 用户登出
     *
     * @return 操作结果
     */
    @Override
    public R<Boolean> logout() {
        Long userId = LoginHelper.getLoginUser().getId();
        LambdaUpdateWrapper<User> luw = Wrappers.lambdaUpdate();
        luw.set(User::getState , 0);
        luw.eq(User::getId , userId);
        update(luw);
        StpUtil.logout(userId);
        return null;
    }

    private boolean checkDuplicateUsername(User user) {
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.eq(User::getUsername, user.getUsername());
        return count(lqw) > 0;
    }


    private boolean checkDuplicateNickname(User user) {
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.eq(User::getNickname, user.getNickname());
        return count(lqw) > 0;
    }

}
